package stego.StegoCaller;

import java.io.*;
import stego.Drivers;

public class Caller_Embedder_JAR
{

    public Caller_Embedder_JAR(File pathTo_JAR, byte arrSrcMsg[], byte arrBatchID[], byte arrCurrFragNum[], byte arrTotalNumFrags[], byte arrSrcFleExtension[], File fleCarrier, 
            File fleSaveDir)
    {
        processCommand(pathTo_JAR, arrSrcMsg, arrBatchID, arrCurrFragNum, arrTotalNumFrags, arrSrcFleExtension, fleCarrier, fleSaveDir);
    }

    public static boolean processCommand(File pathToExternal_JAR, byte arrSourceMsg[], byte arrBatchIdentifier[], byte arrCurrFragmentNum[], byte arrTotalNumFragments[], byte arrSourceFleExtension[], File fleCarrierImg, File fleSaveDirectory)
    {
        if(pathToExternal_JAR != null && pathToExternal_JAR.exists() && pathToExternal_JAR.isFile())
        {
            break MISSING_BLOCK_LABEL_25;
        }
        Drivers.sop("\n\n* * * ERROR * * * Path to External JAR unspecified.");
        return false;
        if(fleCarrierImg != null && fleCarrierImg.exists() && fleCarrierImg.isFile())
        {
            break MISSING_BLOCK_LABEL_53;
        }
        Drivers.sop("\n\n* * * ERROR * * * Carrier Image does not seem to exist.");
        return false;
        if(fleSaveDirectory != null && fleSaveDirectory.exists() && fleSaveDirectory.isDirectory())
        {
            break MISSING_BLOCK_LABEL_81;
        }
        Drivers.sop("\n\n* * * ERROR * * * Save Directory does not seem to exist.");
        return false;
        if(arrSourceMsg != null && arrSourceMsg.length >= 1)
        {
            break MISSING_BLOCK_LABEL_98;
        }
        Drivers.sop("\n\n* * * ERROR * * * arrSourceMsg is not populated.");
        return false;
        if(arrCurrFragmentNum != null && arrCurrFragmentNum.length >= 1)
        {
            break MISSING_BLOCK_LABEL_115;
        }
        Drivers.sop("\n\n* * * ERROR * * * arrCurrFragmentNum is not populated.");
        return false;
        if(arrTotalNumFragments != null && arrTotalNumFragments.length >= 1)
        {
            break MISSING_BLOCK_LABEL_134;
        }
        Drivers.sop("\n\n* * * ERROR * * * arrTotalNumFragments is not populated.");
        return false;
        if(arrSourceFleExtension != null && arrSourceFleExtension.length >= 1)
        {
            break MISSING_BLOCK_LABEL_153;
        }
        Drivers.sop("\n\n* * * ERROR * * * arrSourceFleExtension is not populated.");
        return false;
        if(arrSourceFleExtension != null && arrSourceFleExtension.length >= 1)
        {
            break MISSING_BLOCK_LABEL_172;
        }
        Drivers.sop("\n\n* * * ERROR * * * arrSourceFleExtension is not populated.");
        return false;
        File fleSource;
        String strFleName = (new StringBuilder(String.valueOf(new String(arrBatchIdentifier)))).append("_").append(new String(arrCurrFragmentNum)).append("_").append(new String(arrTotalNumFragments)).toString();
        String extension = new String(arrSourceFleExtension);
        try
        {
            extension = extension.replace("0", "");
        }
        catch(Exception e)
        {
            extension = new String(arrSourceFleExtension);
        }
        fleSource = Drivers.writeImage_Fully((new StringBuilder(String.valueOf(strFleName))).append(".").append(extension).toString(), fleSaveDirectory, arrSourceMsg);
        if(fleSource != null && fleSource.exists())
        {
            break MISSING_BLOCK_LABEL_359;
        }
        Drivers.sop((new StringBuilder("Unable to write File: ")).append(strFleName).append(".").append(extension).append(" at location: ").append(fleSaveDirectory).append(" out to disk").toString());
        return false;
        try
        {
            callJAR_To_Embed(pathToExternal_JAR, fleSource, fleCarrierImg, new String(arrBatchIdentifier), new String(arrCurrFragmentNum), new String(arrTotalNumFragments), fleSaveDirectory);
            try
            {
                fleSource.delete();
                Drivers.sop((new StringBuilder("Successfully deleted temp Fragment File: ")).append(fleSource.getCanonicalPath()).toString());
            }
            catch(Exception e)
            {
                Drivers.sop((new StringBuilder("Unable to Delete File: ")).append(fleSource.getCanonicalPath()).toString());
            }
        }
        catch(Exception e)
        {
            Drivers.eop("processCommand", stego/StegoCaller/Caller_Embedder_JAR.getName(), e, e.getLocalizedMessage(), true);
            return false;
        }
        return true;
    }

    public static boolean callJAR_To_Embed(File flePathToJAR, File fleSource, File fleCarrier, String strBatchID, String strCurrFragNum, String strMaxFragments, File fleSaveDir)
    {
        try
        {
            String strJAR_Name = flePathToJAR.getCanonicalPath().substring(flePathToJAR.getCanonicalPath().lastIndexOf(System.getProperty("file.separator")) + 1);
            String strCommand = (new StringBuilder("java -jar \"")).append(flePathToJAR.getCanonicalPath()).append("\"").append(" -eS ").append("\"").append(fleSource.getCanonicalPath()).append("\"").append(" ").append("\"").append(fleCarrier.getCanonicalPath()).append("\"").append(" ").append(strBatchID).append(" ").append(strCurrFragNum).append(" ").append(strMaxFragments).append(" ").append("\"").append(fleSaveDir.getCanonicalPath()).append("\"").toString();
            if(strCommand != null && !strCommand.trim().toUpperCase().startsWith("JAVA"))
            {
                Drivers.jop_Warning((new StringBuilder("WARNING: The following command: ")).append(strCommand).append("\nDoes not appear to be within expected parameters to launch a JAR file").toString(), "Unverified Command Received");
            }
            Drivers.appendTextToInteractionContent_TextPane((new StringBuilder("Executing the following command: \"")).append(strCommand).append("\"").toString(), true);
            Drivers.sop((new StringBuilder("Executing the following command: \"")).append(strCommand).append("\"").toString());
            Process p = Runtime.getRuntime().exec((new StringBuilder("cmd /c ")).append(strCommand).toString());
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
            for(String line = ""; (line = input.readLine()) != null;)
            {
                Drivers.sop(line);
                Drivers.appendTextToInteractionContent_TextPane(line, true);
                Drivers.sop(line);
            }

            Drivers.appendTextToInteractionContent_TextPane("\nCommand Complete", true);
            Drivers.sop("\nCommand Complete");
        }
        catch(Exception e)
        {
            Drivers.eop("callJAR_To_Embed", stego/StegoCaller/Caller_Embedder_JAR.getName(), e, e.getLocalizedMessage(), true);
            return false;
        }
        return true;
    }
}
